import Vue from 'vue'
import VueRouter from 'vue-router'
//导入登录页
import Login from '@/views/Login';
//导入首页
import Home from '@/views/Home';

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home
  }
]

const router = new VueRouter({
  routes
})
//路由守卫
router.beforeEach(({path}, from, next) => {
  console.log(path);
  if (path == '/login' || path == '/') {
    next();
  } else {
    //如果有token
    if (window.sessionStorage.getItem('vues_token')) {
      //放行
      next();
    }else{
      next('/');//如果没有,返回首页
    }
  }
});

export default router
